package com.EAIPClub.CodeShellConnecter.websocket;

import com.EAIPClub.CodeShellConnecter.ConstantPool;
import com.EAIPClub.CodeShellConnecter.service.WebSSHservice;
import com.api.API.CodeLab.CodeShellManager.CodeShellManagerAPI;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;

/**
 *
 * @Description :WebSSH的WebSocket处理器
 * @Author :崔昌赫
 * @Date :2021/9/12
 *
 */
@Component
@Slf4j
public class WebSSHWebSocketHandler implements WebSocketHandler {
    private WebSSHservice webSSHservice;
    @Autowired
    public void setWebSSHservice(WebSSHservice webSSHservice) {
        this.webSSHservice = webSSHservice;
    }

    private CodeShellManagerAPI api;

    @Autowired
    public void setApi(CodeShellManagerAPI api) {
        this.api = api;
    }

    /**
     * @Description :用户连接上WebSocket的回调
     * @param webSocketSession webSocket会话实例
     * @Author :崔昌赫
     * @Date : 2021/9/12
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession webSocketSession) {
        log.info("用户:{},连接WebSSH",webSocketSession.getAttributes().get(ConstantPool.USER_UUID_KEY));
//        api.setWebSocketSession(webSocketSession.getAttributes().get(ConstantPool.USER_UUID_KEY).toString());
        webSSHservice.initConnection(webSocketSession);
    }

    /**
     * @Description :接受信息回调
     * @param webSocketSession  SSH会话实例
     * @param webSocketMessage  接收到的会话信息
     * @Author :崔昌赫
     * @Date :2021/9/12
     */
    @Override
    public void handleMessage(@NonNull WebSocketSession webSocketSession, @NonNull WebSocketMessage<?> webSocketMessage) {
        if(webSocketMessage instanceof TextMessage){
//            log.info("用户:{},发送命令:{}",webSocketSession.getAttributes().get(ConstantPool.USER_UUID_KEY), webSocketMessage);
            webSSHservice.recvHandle(((TextMessage)webSocketMessage).getPayload(),webSocketSession);
        }else if (webSocketMessage instanceof BinaryMessage){

        }else if (webSocketMessage instanceof PongMessage){

        }else{
            log.error("不可解析的WebSocket信息类型:"+webSocketMessage);
        }
    }

    /**
     * @Description :出现错误的回调
     * @param webSocketSession SSH会话实例
     * @param throwable 抛出的错位
     * @Author :崔昌赫
     * @Date :2020/9/12
     */
    @Override
    public void handleTransportError(@NonNull WebSocketSession webSocketSession, @NonNull Throwable throwable) {
        log.error("数据传输错误！{}");
        throwable.printStackTrace();
    }

    /**
     * @Description :连接关闭回调
     * @param webSocketSession SSH会话实例
     * @param closeStatus 关闭状态
     * @Author :崔昌赫
     * @Date :2021/9/12
     */
    @Override
    public void afterConnectionClosed(WebSocketSession webSocketSession, @NonNull CloseStatus closeStatus) {
        log.info("用户:{}断开webssh连接", webSocketSession.getAttributes().get(ConstantPool.USER_UUID_KEY));
        api.closeWebSocketSession(webSocketSession.getAttributes().get(ConstantPool.USER_UUID_KEY).toString());
        webSSHservice.close(webSocketSession);
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}
